package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreInitExcution;
import com.fortune.dto.in.mStoreInit.MStoreInitAddDto;
import com.fortune.dto.in.mStoreInit.MStoreInitEditDto;
import com.fortune.dto.in.mStoreInit.MStoreInitListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreInitEnum;
import com.fortune.service.MProductInfoService;
import com.fortune.service.MStoreInitService;
import com.fortune.service.MStoreService;
import com.fortune.vo.channelDiscountMgt.ProductInChannealDiscountMgt;
import com.fortune.vo.mStoreInit.MInitInfo;
import com.fortune.vo.mStoreInit.MInitStoreName;
import com.fortune.vo.mStoreInit.MinitDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "mStoreInit", description = "仓库初始化", tags = {"仓库初始化"})
@RestController
@RequestMapping("mStoreInit")
public class MStoreInitController {
	@Autowired
	private MStoreInitService mStoreInitService;
	@Autowired
	private MStoreService mStoreService;
	@Autowired
	private MProductInfoService mProductInfoService;
	
	@ApiOperation(value = "初始化单列表", nickname = "mStoreInitList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MInitInfo>> list(HttpServletRequest request,@ApiIgnore @Validated MStoreInitListDto mStoreInitListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		mStoreInitListDto.setPage((mStoreInitListDto.getPage()-1)*10);
		List<MInitInfo> list = mStoreInitService.queryMInitInfoList(companyId,mStoreInitListDto);
		int count = mStoreInitService.queryMInitInfoListCount(companyId);
		PagingData<MInitInfo> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new MStoreInitExcution<PagingData<MInitInfo>>(pagingData, MStoreInitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加初始化单", nickname = "mStoreInitAdd", notes = "", tags={ "@石匡" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MStoreInitAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		String userId = mUserInfo.getId();
		int num = mStoreInitService.queryMInitInfoIsExist(companyId,addDto.getNum(),addDto.getStoreId());
		if (num > 0){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.EXIST);
		}
		num = mStoreInitService.addMInitInfo(userId,companyId,addDto);
		if (num < 1){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.FAILURE);
		}
		return new MStoreInitExcution<Void>(null, MStoreInitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑初始化单", nickname = "mStoreInitEdit", notes = "", tags={ "@石匡" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MStoreInitEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mStoreInitService.queryEditMInitInfoIsExist(companyId,editDto.getNum(),editDto.getStoreId());
		if (num > 0){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.EXIST);
		}
		num = mStoreInitService.editMInitInfo(companyId,editDto);
		if (num < 1){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.FAILURE);
		}
		return new MStoreInitExcution<Void>(null, MStoreInitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除初始化单", nickname = "mStoreInitDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="初始化单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mStoreInitService.deleteMInitInfo(companyId,id);
		if (num < 1){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.FAILURE);
		}
		return new MStoreInitExcution<Void>(null, MStoreInitEnum.SUCCESS);
	}

	@ApiOperation(value = "初始化单详情", nickname = "mStoreInitInfo", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
	@GetMapping(value = "info",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MinitDetails> info(HttpServletRequest request,@Validated @NotBlank(message="初始化单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		MinitDetails minitDetails = mStoreInitService.queryStoreInitDetails(companyId,id);
		return new MStoreInitExcution<MinitDetails>(minitDetails, MStoreInitEnum.SUCCESS);
	}
	
	@ApiOperation(value = "确认初始化单", nickname = "mStoreInitConfirm", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "confirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @NotBlank(message="初始化单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mStoreInitService.storeInitConfirm(companyId,id);
		if (num <1){
			return new MStoreInitExcution<Void>(null, MStoreInitEnum.FAILURE);
		}
		return new MStoreInitExcution<Void>(null, MStoreInitEnum.SUCCESS);
	}

	@ApiOperation(value = "获取厂商仓库", nickname = "mStoreName", notes = "", tags={ "@石匡" })
	@PutMapping(value = "getStoreName",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MInitStoreName>> getStoreName(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<MInitStoreName> list = mStoreService.queryStoreNameList(companyId);
		return new MStoreInitExcution<List<MInitStoreName>>(list, MStoreInitEnum.SUCCESS);
	}

	@ApiOperation(value = "获取商品名称编号", nickname = "mProductInfo", notes = "", tags={ "@石匡" })
	@PutMapping(value = "mProductInfo",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProductInChannealDiscountMgt>> mProductInfo(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<ProductInChannealDiscountMgt> list = mProductInfoService.queryProduceDiscount(companyId);
		return new MStoreInitExcution<List<ProductInChannealDiscountMgt>>(list, MStoreInitEnum.SUCCESS);
	}
}
